/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.

// TriggerSpec defines the desired state of Trigger

type TriggerSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	// Foo is an example field of Trigger. Edit Template_types.go to remove/update
	CreationConfigs   []CreationConfig   `json:"creationConfigs,flow,omitempty"`
	MutationConfigs   []MutationConfig   `json:"mutationConfigs,flow,omitempty"`
	SideEffectConfigs []SideEffectConfig `json:"sideEffectConfigs,flow,omitempty"`
}

type SideEffectConfig struct {
	Name         string   `json:"name"`
	Match        Match    `json:"match"`
	TemplateRefs []string `json:"templateRefs"`
	// +kubebuilder:default=false
	Recreate bool `json:"recreate,omitempty"`
}

type CreationConfig struct {
	Name                string               `json:"name"`
	LabelSelector       metav1.LabelSelector `json:"labelSelector,omitempty"`
	AnnotationNamespace string               `json:"annotationNamespace,omitempty"`
	AnnotationTrigger   string               `json:"annotationTrigger,omitempty"`
	TemplateRefs        []string             `json:"templateRefs"`
	Action              string               `json:"action,omitempty"`
	DisableUniqueName   string               `json:"disableUniqueName,omitempty"`
}

type MutationConfig struct {
	Name string `json:"name"`

	// +kubebuilder:validation:Enum=merge;replace
	// +kubebuilder:default=merge
	UpdateStrategy string `json:"updateStrategy,omitempty"`

	LabelSelector       metav1.LabelSelector `json:"labelSelector,omitempty"`
	AnnotationNamespace string               `json:"annotationNamespace,omitempty"`
	AnnotationTrigger   string               `json:"annotationTrigger,omitempty"`
	Containers          []string             `json:"containers,flow,omitempty"`
	IgnoreNamespaces    []string             `json:"ignoreNamespaces,flow,omitempty"`
	Annotations         []string             `json:"annotations,flow,omitempty"`
	Volumes             []string             `json:"volumes,flow,omitempty"`
	TemplateRefs        []string             `json:"templateRefs,flow,omitempty"`
	ObjectSelector      ObjectSelector       `json:"objectSelector,omitempty"`
	Match               Match                `json:"match,omitempty"`
}

type ObjectSelector struct {
	Kind string `json:"kind"`
	Name string `json:"name"`
}

type AnnotationConfig struct {
	VolumeMounts []VolumeMountAnnotation `json:"volumeMounts,omitempty" patchStrategy:"merge" patchMergeKey:"mountPath" protobuf:"bytes,1,rep,name=volumeMounts"`
}

type VolumeMountAnnotation struct {
	Name              string   `json:"name" protobuf:"bytes,1,opt,name=name"`
	InitContainerRefs []string `json:"initContainerRefs" protobuf:"bytes,2,opt,name=initContainerRefs"`
	ContainerRefs     []string `json:"containerRefs" protobuf:"bytes,3,opt,name=containerRefs"`
}

// TriggerStatus defines the observed state of Trigger
type TriggerStatus struct {
	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	Phase      Phase       `json:"phase,omitempty" protobuf:"bytes,1,opt,name=phase,casttype=Phase"`
	Conditions []Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,2,rep,name=conditions"`
}

//+kubebuilder:object:root=true

// Trigger is the Schema for the Triggers API
// +kubebuilder:subresource:status
type Trigger struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   TriggerSpec   `json:"spec,omitempty"`
	Status TriggerStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TriggerList contains a list of Trigger
type TriggerList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []Trigger `json:"items"`
}

func init() {
	SchemeBuilder.Register(&Trigger{}, &TriggerList{})
}

func (in *Trigger) GetNamespacedName() string {
	return fmt.Sprintf("%s/%s", in.Namespace, in.Name)
}
